<template>
    <view>
        <!-- 头部 -->
        <view class="head-box">
            <view class="head-item oh tc">
                <image @click="preview_event" class="avatar" :src="avatar" mode="widthFix" />
                <text class="item-name dis-block cr-fff">积分：{{ user_score }}</text>
            </view>

            <view v-if="head_nav_list.length > 0" class="head-nav oh wh-auto" style="display: none">
                <block v-for="(item, index) in head_nav_list" :key="index">
                    <navigator :url="'/pages/' + item.url + '/' + item.url" hover-class="none">
                        <view class="head-nav-item tc fl">
                            <view>{{ item.count }}</view>
                            <view>{{ item.name }}</view>
                        </view>
                    </navigator>
                </block>
            </view>

            <view class="message-nav" style="display: none">
                <navigator url="/pages/message/message" hover-class="none">
                    <image :src="scorestore_static_url+'mini/user-head-message-icon.png'" mode="aspectFill" />
                    <text>消息</text>
                    <text>{{ message_total }}</text>
                </navigator>
            </view>
        </view>

        <!-- 导航 -->
        <view class="nav-box bg-white">
            <block v-for="(item, index) in nav_lists" :key="index">
                <view v-if="item.is_show == 1 && item.url == 'order'">
                    <navigator :url="'/pages/fcc/scorestore/' + item.url + '/' + item.url" class="nav-item br-b" hover-class="none">
                        <view class="arrow-right">
                            <image :src="scorestore_static_url +'mini/'+ item.icon + '.png'" class="item-icon" mode="widthFix" />
                            <text class="item-name">{{ item.name }}</text>
                        </view>
                    </navigator>
                </view>

                <view v-if="item.is_show == 1 && item.url != 'order'">
                    <navigator :url="'/pages/' + item.url + '/' + item.url" class="nav-item br-b" hover-class="none">
                        <view class="arrow-right">
                            <image :src="scorestore_static_url +'mini/'+ item.icon + '.png'" class="item-icon" mode="widthFix" />
                            <text class="item-name">{{ item.name }}</text>
                        </view>
                    </navigator>
                </view>

                <view v-if="item.url == 'order' && user_order_status_list.length > 0" class="items-list br-b oh">
                    <block v-for="(items, index1) in user_order_status_list" :key="index1">
                        <navigator :url="'/pages/fcc/scorestore/' + item.url + '/' + item.url + '?status=' + items.status" hover-class="none">
                            <view class="items fl tc">
                                <view class="badge-icon">
                                    <component-badge :prop-number="items.count"></component-badge>
                                </view>
                                <image :src="scorestore_static_url+'mini/user-index-nav-order-icon-' + items.status + '.png'" class="items-icon" mode="aspectFill" />
                                <view class="items-name">{{ items.name }}</view>
                            </view>
                        </navigator>
                    </block>
                </view>
            </block>
        </view>

        <!-- 结尾 -->
        <component-footer :prop-status="3"></component-footer>
		<!-- #ifdef APP-PLUS -->
		<yk-authpup ref="authpup" type="top" @changeAuth="changeAuth" :permissionID="permissionID"></yk-authpup>
		<!-- #endif -->
    </view>
</template>

<script>
const app = getApp();
// #ifdef APP-PLUS
import ykAuthpup from "@/components/yk-authpup/yk-authpup";
// #endif
import componentBadge from '@/components/badge/badge';
import componentIconNav from '@/components/icon-nav/icon-nav';
import componentBanner from '@/components/slider/slider';
import componentFooter from "@/pages/fcc/scorestore/public/footer";
import componentNoData from "@/components/no-data/no-data";
import componentBottomLine from "@/components/bottom-line/bottom-line";
import componentOnlineService from "@/components/online-service/online-service";

var scorestore_static_url = app.globalData.get_static_url('scorestore', true);
var common_static_url = app.globalData.get_static_url('common');
export default {
    components: {
		// #ifdef APP-PLUS
		ykAuthpup,
		// #endif
        componentBadge,
		componentIconNav,
		componentBanner,
		componentFooter,
		componentNoData,
		componentBottomLine,
		componentOnlineService
    },
    data() {
        return {
			scorestore_static_url: scorestore_static_url,
			common_static_url: common_static_url,
            avatar: app.globalData.data.default_user_head_src,
            nickname: '用户名',
            customer_service_tel: null,
            common_user_center_notice: null,
            message_total: 0,
            head_nav_list: [
                {
                    name: '订单总数',
                    url: 'user-order',
                    count: 0
                },
                {
                    name: '商品收藏',
                    url: 'user-faovr',
                    count: 0
                },
                {
                    name: '我的足迹',
                    url: 'user-goods-browse',
                    count: 0
                },
                {
                    name: '我的积分',
                    url: 'user-integral',
                    count: 0
                }
            ],
            user_order_status_list: [
                {
                    name: '待支付',
                    status: 1,
                    count: 0
                },
                {
                    name: '待发货',
                    status: 2,
                    count: 0
                },
                {
                    name: '待收货',
                    status: 3,
                    count: 0
                },
                {
                    name: '已完成',
                    status: 4,
                    count: 0
                }
            ],
            nav_lists: [
                {
                    url: 'order',
                    icon: 'user-nav-order-icon',
                    is_show: 1,
                    name: '我的订单'
                },
                {
                    url: 'user-address',
                    icon: 'user-nav-address-icon',
                    is_show: 1,
                    name: '我的地址'
                }
            ],
            user_score: 0,
			permissionID:''
        };
    },
    onShow() {
        app.globalData.page_event_onshow_handle();
        this.init();
    },
    // 下拉刷新
    onPullDownRefresh(e) {
        this.init(e);
    },
    methods: {
		// 获取数据
		init(e) {
		    var user = app.globalData.get_user_info(this, "init");
		    if (user != false) {
		        this.set_user_base(user);
		        this.get_data();
		    }
		},
		
		// 设置用户基础信息
		set_user_base(user) {
		    if((user.avatar || null) != null) {
		        this.setData({"avatar": user.avatar});
		    }
		    if((user.user_name_view || null) != null) {
		        this.setData({"nickname": user.user_name_view});
		    }
		},

        // 获取数据
        get_data() {
            uni.request({
                url: app.globalData.get_request_url('center', 'index', 'scorestore'),
                method: 'POST',
                data: {},
                dataType: 'json',
                header: {
                    'content-type': 'application/x-www-form-urlencoded'
                },
                success: (res) => {
                    uni.stopPullDownRefresh();
                    console.log(res);
					
                    if (res.data.code == 0) {
                        var data = res.data.data; // 订单数量处理

                        var temp_user_order_status_list = this.user_order_status_list;

                        if ((data.user_order_status || null) != null && data.user_order_status.length > 0) {
                            for (var i in temp_user_order_status_list) {
                                for (var t in data.user_order_status) {
                                    if (temp_user_order_status_list[i]['status'] == data.user_order_status[t]['status']) {
                                        temp_user_order_status_list[i]['count'] = data.user_order_status[t]['count'];
                                        break;
                                    }
                                }
                            }
                        } // 头部导航总数

                        var temp_head_nav_list = this.head_nav_list;

                        if ((data.user_order_count || 0) == 0) {
                            temp_head_nav_list[0]['count'] = 0;
                        } else {
                            temp_head_nav_list[0]['count'] = data.user_order_count;
                        }

                        if ((data.user_goods_favor_count || 0) == 0) {
                            temp_head_nav_list[1]['count'] = 0;
                        } else {
                            temp_head_nav_list[1]['count'] = data.user_goods_favor_count;
                        }

                        if ((data.user_goods_browse_count || 0) == 0) {
                            temp_head_nav_list[2]['count'] = 0;
                        } else {
                            temp_head_nav_list[2]['count'] = data.user_goods_browse_count;
                        }

                        if ((data.integral || 0) == 0) {
                            temp_head_nav_list[3]['count'] = 0;
                        } else {
                            temp_head_nav_list[3]['count'] = data.integral;
                        }

                        this.setData({
                            user_order_status_list: temp_user_order_status_list,
                            customer_service_tel: data.customer_service_tel || null,
                            common_user_center_notice: data.common_user_center_notice || null,
                            avatar: data.avatar != null ? data.avatar : (this.avatar || null) == null ? app.globalData.data.default_user_head_src : this.avatar,
                            nickname: data.nickname != null ? data.nickname : this.nickname,
                            message_total: (data.common_message_total || 0) == 0 ? 0 : data.common_message_total,
                            head_nav_list: temp_head_nav_list,
                            //'nav_lists[3].is_show': (data.common_app_is_enable_answer == 1) ? 1 : 0,
                            user_score: (data.integral || 0) == 0 ? 0 : data.integral
                        });
                    } else {
                        app.globalData.showToast(res.data.msg);
                    }
                },
                fail: () => {
                    uni.stopPullDownRefresh();
                    app.globalData.showToast(this.$t('common.internet_error_tips'));
                }
            });
        },

        // 清除缓存
        clear_storage(e) {
            uni.clearStorage();
            app.globalData.showToast('清除缓存成功', 'success');
        },

        // 客服电话
        call_event() {
            if (this.customer_service_tel == null) {
                app.globalData.showToast(this.$t('setup.setup.utnr7g'));
            } else {
				if(app.globalData.is_android())
				{
					this.permissionID = 'CALL_PHONE';
					setTimeout(()=>{
						this.$refs['authpup'].open();
					},500);
				}else{
					app.globalData.call_tel(this.customer_service_tel);
				}
            }
        },
		// #ifdef APP-PLUS
		//用户授权权限后的回调
		changeAuth(e){
			if(this.permissionID == 'WRITE_EXTERNAL_STORAGE')
			{
				// 直接下载
				app.globalData.save_image_by_url(e);
			}else{
				app.globalData.call_tel(this.customer_service_tel);
			}
		},
		// #endif

        // 头像查看
        preview_event() {
            if (app.globalData.data.default_user_head_src != this.avatar) {
				if(app.globalData.is_android())
				{
					var self = this;
					uni.previewImage({
					    current: 0,
					    urls: [self.avatar],
						longPressActions: {
							itemList: [self.$t('fengcheche.fcctips.t9')],
							success: function(data) {
								uni.closePreviewImage();
								self.permissionID = 'WRITE_EXTERNAL_STORAGE';
								setTimeout(()=>{
									self.$refs['authpup'].open(self.avatar);
								},500);
							},
							fail: function(err) {
								console.log(err.errMsg);
							}
						}
					});
				}else{
					uni.previewImage({
					    current: 0,
					    urls: [this.avatar]
					});
				}
            }
        },

        // 导航事件
        navigation_event(e) {
            app.globalData.operation_event(e);
        },

        // 头像加载错误
        user_avatar_error(e) {
            this.setData({
                avatar: app.globalData.data.default_user_head_src
            });
        }
    }
};
</script>
<style>
@import './my.css';
</style>
